#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>
#include <QUuid>
#include <QSqlError>
#include "json.h"

int main(int argc, char** argv)
{
	QCoreApplication a(argc, argv);
	QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
	db.setDatabaseName("data.db");
	if (!db.open())
		return 0;

	QString sql = "create table if not exists test(id char(16) primary key,"
		"name char(50))";
	QSqlQuery query;
	if (!query.exec(sql))
	{
		qDebug() << "create database failed.";
		return 0;
	}

	sql = "insert into test(id, name)values(:id, :name)";
	QUuid uuid = QUuid::createUuid();
	QString name = "John Smith";
	QSqlQuery insert;
	insert.prepare(sql);
	insert.bindValue(":id", uuid.toRfc4122());
	insert.bindValue(":name", name);
	if(!insert.exec())
	{
		qDebug() << "insert value failed.\n" << query.lastError().databaseText();
		return 0;
	}
		
	QSqlQuery select;
	select.exec("select * from test");
	while(select.next())
	{
		qDebug() << select.value(0).toString();
		qDebug() << select.value(1).toString();
	}

	return 0;
}
